/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fplaton.ui;

import static fplaton.db.SQLiteInteractionUI.buildTableModel;
import java.sql.ResultSet;
import fplaton.db.SQLiteJDBC;
import java.awt.Component;
import javax.swing.JOptionPane;
import java.sql.PreparedStatement;

/**
 *
 * @author evanmana
 */
public class EditClassForm extends javax.swing.JFrame {

    /**
     * Creates new form EditClassForm
     */
    public void ViewTable(){
        Component frame = null;
        
         try {
            SQLiteJDBC connection = new SQLiteJDBC("frontistirio.db");
            ResultSet rs = connection.executeQuery("select * from CLASS");
            jTableViewClasses = new javax.swing.JTable(buildTableModel(rs));

            jScrollPaneViewClasses.setViewportView(jTableViewClasses);
        }
        catch (Exception SQLException) {
             JOptionPane.showMessageDialog(frame, "Αδυναμία σύνδεσης με την βάση.", "Προβολή Τμημάτων", JOptionPane.ERROR_MESSAGE);
        }
        
    }
    
    public void DeleteRecord(){
        Component frame = null;
        int pos = jTableViewClasses.getSelectedRow();
        int id;
        String a;
        
        id= (int) jTableViewClasses.getValueAt(pos, 0);
        
        a=Integer.toString(id);
                
        try {
                SQLiteJDBC connection = new SQLiteJDBC("frontistirio.db");

                PreparedStatement prep = connection.conn.prepareStatement("DELETE FROM CLASS WHERE ID=?");
                prep.setString(1, a);
                

                connection.conn.setAutoCommit(false);
                prep.execute();
                connection.conn.setAutoCommit(true);
                
                JOptionPane.showMessageDialog(frame,"Το τμήμα διαγράφηκε.");
        }
        catch (Exception SQLException) {
             JOptionPane.showMessageDialog(frame, "Αδυναμία σύνδεσης με την βάση.", "Διαγραφή Τμημάτων", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    
    public EditClassForm() {
        initComponents();
        ViewTable();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPaneViewClasses = new javax.swing.JScrollPane();
        jTableViewClasses = new javax.swing.JTable();
        jButtonNewClass = new javax.swing.JButton();
        jButtonDeleteRecord = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Τμήματα");

        jTableViewClasses.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableViewClasses.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableViewClassesMouseClicked(evt);
            }
        });
        jScrollPaneViewClasses.setViewportView(jTableViewClasses);

        jButtonNewClass.setText("Νέο Τμήμα");
        jButtonNewClass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewClassActionPerformed(evt);
            }
        });

        jButtonDeleteRecord.setText("Διαγραφή Τμήματος");
        jButtonDeleteRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteRecordActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPaneViewClasses, javax.swing.GroupLayout.PREFERRED_SIZE, 901, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonNewClass)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonDeleteRecord)))
                .addContainerGap(127, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPaneViewClasses, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonNewClass)
                    .addComponent(jButtonDeleteRecord))
                .addContainerGap(548, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonNewClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewClassActionPerformed
        InsertClass InsertClassForm = new InsertClass();
        InsertClassForm.setVisible(true);
    }//GEN-LAST:event_jButtonNewClassActionPerformed

    private void jButtonDeleteRecordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteRecordActionPerformed
        DeleteRecord();
        ViewTable();
    }//GEN-LAST:event_jButtonDeleteRecordActionPerformed

    private void jTableViewClassesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableViewClassesMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jTableViewClassesMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditClassForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditClassForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditClassForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditClassForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditClassForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonDeleteRecord;
    private javax.swing.JButton jButtonNewClass;
    private javax.swing.JScrollPane jScrollPaneViewClasses;
    private javax.swing.JTable jTableViewClasses;
    // End of variables declaration//GEN-END:variables
}
